<?php
/**
 *队列管理
 *
 * @author 佘雨行 <syh794@foxmail.com>
 */
use Common\Bridge;

define('SITE_AUTH', 'tool-queue');
require('../../../common.inc.php');

class queue extends \lib\core\AdminAction
{
    /**
     *显示队列全部列名
     */
    public function doDefault()
    {
        $queue = loadconf('tool/queue');
        $redis = Bridge::redis(Bridge::REDIS_QUEUE_R);
        //计算每个队列的长度
        foreach ($queue as $k => $v) {
            $size = $redis->lLen($v['ename']);
            $queue[$k]['size'] = $size;
        }

        $smarty = $this->app->page();
        $smarty->value('data',$queue);

        $smarty->output('tool/queue.tpl');
    }

    /**
     * 显示队列内容
     */
    public function doContent() {
        $rangename = $_GET['ename'];
        $rangesize = $_GET['size'];
        $page = max(1, intval($_GET['page']));
        $redis = Bridge::redis(Bridge::REDIS_QUEUE_R);
        //列表中的元素
        $range = $redis->lRange($rangename, ($page-1) * self::PAGE_SIZE, $page * self::PAGE_SIZE);
        $smarty = $this->app->page();
        $smarty->value('rangename', $rangename);
        $smarty->value('range', $range);
        $pagination = '';
        if (empty($rangename)) {
            $pagination = $this->pagination($rangesize, self::PAGE_SIZE, $page);
        }
        $smarty->value('pagination', $pagination);
        $smarty->output('tool/queuecontent.tpl');
    }

    /**
     *清除操作
     */
    public function doDelete() {
        $rangename = trim($_POST['rangename']);
        $redis = Bridge::redis(Bridge::REDIS_QUEUE_W);
        $redis->delete($rangename);
        $this->output(['title' => '操作成功', 'type' => 'success']);
    }
}
$app->run();